﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Acms.Exceptions
{
    [global::System.Serializable]
    public class ValidationException : Exception
    {
        //
        // For guidelines regarding the creation of new exception types, see
        //    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconerrorraisinghandlingguidelines.asp
        // and
        //    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp07192001.asp
        //

        public ValidationException() { }
        public ValidationException(string message) : base(message) 
        {
            Add(message);
        }
        public ValidationException(string message, Exception inner) : base(message, inner) 
        {
            Add(message);
        }
        protected ValidationException(
          System.Runtime.Serialization.SerializationInfo info,
          System.Runtime.Serialization.StreamingContext context)
            : base(info, context) { }

        private List<ValidationExceptionItem> items = new List<ValidationExceptionItem>();
        public ValidationExceptionItem[] Items
        {
            get { return items.ToArray(); }
        }

        public void Add(string message)
        {
            Add(new ValidationExceptionItem(message));
        }

        public void Add(ValidationExceptionItem item)
        {
            items.Add(item);
        }

        public override string Message
        {
            get
            {
                StringBuilder tmp = new StringBuilder();
                string spacer = "";
                foreach (ValidationExceptionItem item in items)
                {
                    tmp.Append(spacer);
                    tmp.Append(item.Message);
                    spacer = ":\n";
                }
                return tmp.ToString();
            }
        }
    }

    public class ValidationExceptionItem : Exception
    {
        public ValidationExceptionItem() { }
        public ValidationExceptionItem(string message) : base(message) { }
        public ValidationExceptionItem(string message, Exception inner) : base(message, inner) { }
        protected ValidationExceptionItem(
          System.Runtime.Serialization.SerializationInfo info,
          System.Runtime.Serialization.StreamingContext context)
            : base(info, context) { }

    }
}
